package com.ruoyi.gczl.sybg.ws.ylb.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gczl.sybg.ws.ylb.domain.SybgYlbws;
import com.ruoyi.gczl.sybg.ws.ylb.mapper.SybgYlbwsMapper;
import com.ruoyi.gczl.sybg.ws.ylb.service.ISybgYlbwsService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 试验报告硬梁包无损Service业务层处理
 *
 * @author huacai
 * @date 2024-08-23
 */
@Service
public class SybgYlbwsServiceImpl implements ISybgYlbwsService {
    @Autowired
    private SybgYlbwsMapper sybgYlbwsMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询试验报告硬梁包无损
     *
     * @param sybgYlbwsId 试验报告硬梁包无损主键
     * @return 试验报告硬梁包无损
     */
    @Override
    public SybgYlbws selectSybgYlbwsBySybgYlbwsId(Integer sybgYlbwsId) {
        return sybgYlbwsMapper.selectSybgYlbwsBySybgYlbwsId(sybgYlbwsId);
    }

    /**
     * 查询试验报告硬梁包无损列表
     *
     * @param sybgYlbws 试验报告硬梁包无损
     * @return 试验报告硬梁包无损
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<SybgYlbws> selectSybgYlbwsList(SybgYlbws sybgYlbws) {
        return sybgYlbwsMapper.selectSybgYlbwsList(sybgYlbws);
    }

    /**
     * 新增试验报告硬梁包无损
     *
     * @param sybgYlbws 试验报告硬梁包无损
     * @return 结果
     */
    @Override
    public int insertSybgYlbws(SybgYlbws sybgYlbws) {
        sybgYlbws.setCreateTime(DateUtils.getNowDate());
        return sybgYlbwsMapper.insertSybgYlbws(sybgYlbws);
    }

    /**
     * 批量新增试验报告硬梁包无损
     *
     * @param sybgYlbwss 试验报告硬梁包无损List
     * @return 结果
     */
    @Override
    public int batchInsertSybgYlbws(List<SybgYlbws> sybgYlbwss) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(sybgYlbwss)) {
            try {
                for (int i = 0; i < sybgYlbwss.size(); i++) {
                    int row = sybgYlbwsMapper.insertSybgYlbws(sybgYlbwss.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == sybgYlbwss.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改试验报告硬梁包无损
     *
     * @param sybgYlbws 试验报告硬梁包无损
     * @return 结果
     */
    @Override
    public int updateSybgYlbws(SybgYlbws sybgYlbws) {
        sybgYlbws.setUpdateTime(DateUtils.getNowDate());
        return sybgYlbwsMapper.updateSybgYlbws(sybgYlbws);
    }

    /**
     * 批量删除试验报告硬梁包无损
     *
     * @param sybgYlbwsIds 需要删除的试验报告硬梁包无损主键
     * @return 结果
     */
    @Override
    public int deleteSybgYlbwsBySybgYlbwsIds(Integer[] sybgYlbwsIds) {
        return sybgYlbwsMapper.deleteSybgYlbwsBySybgYlbwsIds(sybgYlbwsIds);
    }

    /**
     * 删除试验报告硬梁包无损信息
     *
     * @param sybgYlbwsId 试验报告硬梁包无损主键
     * @return 结果
     */
    @Override
    public int deleteSybgYlbwsBySybgYlbwsId(Integer sybgYlbwsId) {
        return sybgYlbwsMapper.deleteSybgYlbwsBySybgYlbwsId(sybgYlbwsId);
    }
}
